package ThreadTest;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class CallableTest implements Callable {

    private int ticket=10;

    @Override
    public String call() throws Exception {

        for (int x=0;x<100;x++){
            if (this.ticket>0){
                System.out.println("卖票,ticket："+ticket--);
            }
        }

        return "票卖完了";
    }

    public static void main(String[] args) throws Exception {
        CallableTest c1=new CallableTest();
        CallableTest c2=new CallableTest();
        FutureTask<String> t1 = new FutureTask<String>(c1);
        FutureTask<String> t2 = new FutureTask<String>(c2);
        //FutureTask是Runnable接口子类，所以可以使用Thread类的构造来接收task对象
        new  Thread(t1).start();//启动多线程
        new Thread(t2).start();//启动多线程
        //多线程执行完毕后可以取得内容，依靠FutureTask的父接口future中的get（）方法。
        System.out.println("A线程返回结果："+t1.get());
        System.out.println("B线程返回结果："+t2.get());
    }


}
